package com.atguigu.rpc.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * PRC slave demo
 *
 * @Author 剧情再美终是戏
 * @Date 2020/1/3
 * @return
 * @Version 1.0
 **/
public class Slave {

    public static void main(String[] args) throws IOException {

        // 创建serverSockets实例
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端启动成功....");

        // 一直接master的请求
        while (true) {
            Socket accept = serverSocket.accept();
            System.out.println("接收到客户端的连接");

            // 接收客户端的命令
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream(), "UTF-8"));
            String command = bufferedReader.readLine();
            System.out.println("接收到客户商的命令为" + command);

            // 执行客户端的命令
            Runtime.getRuntime().exec(command);

            // 关闭资源
            accept.close();

        }
    }

}
